import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;


/**
 * This panel is responsible for helping planes
 * taxi to and from runways.
 *
 * From Komdetch:
 * This panel will interact with the FD/CD, Local Control, and the Traffic Manager panels. This panel should have a view of the airport, but is only responsible for the "taxi" areas of the runways, not the takeoff/landing strips.
 * There will be lists of planes that wish to take off and land (this list should be sorted by priority as given by the Traffic Manager (that panel will coordinate the order in which planes can land/take off).
 * This panel is responsible for routing planes from the runway to the terminal and vice versa.  I'm not sure how this is to be done, we could have a "Draw a path" system, or something else.
 * When a plane is routed to the runway for takeoff, there will be a button to "clear the plane for takeoff", which will send the clear signal to FD/CD.
 * @author Henry Yuen
 *
 */
public class TrafficManagerPanel extends JPanel{

	private ArrivalsTableModel arrivalsTableModel;	//this is the data container for all the planes arriving
	private DeparturesTableModel departuresTableModel; //this is the data container for all the planes department

	//ARRIVING PLANE ACTIONS
	private JButton clearToLandButton;
	private JButton messagePilotButton;

	private JButton notifyOtherUsersButton;
	private JButton messagePilotButton2;

	//DEPARTING PLANE ACTIONS
	private JButton clearForTakeoffButton;



	public TrafficManagerPanel() {

		// initialize the ui components
		initUI();
	}

	public void initUI() {
		// set the layout
		setLayout(new GridLayout(3,1));

		//create the buttons
		clearToLandButton = new JButton("Clear to Land");
		messagePilotButton = new JButton("Message Pilot");

		clearForTakeoffButton = new JButton("Clear for Takeoff");
		messagePilotButton2 = new JButton("Message Pilot");

		// Create arrivals table
		arrivalsTableModel = new ArrivalsTableModel();
    	JTable arrivalsTable = new JTable(arrivalsTableModel);
    	arrivalsTable.setPreferredScrollableViewportSize(new Dimension(600, 60));

    	// create arrivals scroll pane and add the arrivals table to it
    	JScrollPane arrivalsScrollPane = new JScrollPane(arrivalsTable);

    	// create arrivals actions panel
    	JPanel arrivalsActionsPanel = new JPanel();
    	arrivalsActionsPanel.add(clearToLandButton);
    	arrivalsActionsPanel.add(messagePilotButton);

    	// create arrivals panel and add the arrivals scroll pane and actions panel to it
    	JPanel arrivalsPanel = new JPanel();
    	arrivalsPanel.setLayout(new BoxLayout(arrivalsPanel, BoxLayout.Y_AXIS));
    	arrivalsPanel.setBorder(BorderFactory.createTitledBorder("Arrivals"));
    	arrivalsPanel.add(arrivalsScrollPane);
    	arrivalsPanel.add(arrivalsActionsPanel);

    	// create departures table
    	departuresTableModel = new DeparturesTableModel();
    	JTable departuresTable = new JTable(departuresTableModel);
    	departuresTable.setPreferredScrollableViewportSize(new Dimension(600, 60));

    	// create departures scroll pane and add the departures table to it
    	JScrollPane departuresScrollPane = new JScrollPane(departuresTable);

    	// create departures actions panel
    	JPanel departuresActionsPanel = new JPanel();
    	departuresActionsPanel.add(clearForTakeoffButton);
    	departuresActionsPanel.add(messagePilotButton2);

    	// create departures panel and add the departures scroll pane and actions panel to it
    	JPanel departuresPanel = new JPanel();
    	departuresPanel.setLayout(new BoxLayout(departuresPanel, BoxLayout.Y_AXIS));
    	departuresPanel.setBorder(BorderFactory.createTitledBorder("Departures"));
    	departuresPanel.add(departuresScrollPane);
    	departuresPanel.add(departuresActionsPanel);


		add(arrivalsPanel);
		add(departuresPanel);
		add(new RiskMitigationPanel());
	}

	// All table information goes in here.
    class ArrivalsTableModel extends AbstractTableModel {
    	// Three different columns:  User's name, position, and online status
    	String[] columnNames = {"Plane ID", "Origin", "Runway", "Location", "Status"};

    	// Sample data.  Data entered is a two dimensional array of objects.
    	Object[][] data = {
    			{"GFS102","Hobby","KAP303","Runway 3","Landing"},
    			{"SGM304","JFK","SAL200","Runway 4","In Air"},
    	};

    	// Essential functions

        public int getColumnCount() {
        	return columnNames.length;
        }

        public int getRowCount() {
        	if (data == null) return 0;
        	return data.length;
        }

        public String getColumnName(int col) {
        	return columnNames[col];
        }

        // Returns the object at the indicated position
        public Object getValueAt(int row, int col) {
        	if (data == null) return 0;
            return data[row][col];
        }

        // Turns booleans into checkboxes.  Don't ask.  I don't know how it works myself.
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        // The table cannot be edited by the user, no matter what.
        public boolean isCellEditable(int row, int col) {
           	return false;
        }

        // Edits the value at the indicated position
        public void setValueAt(Object value, int row, int col) {
        	if (data == null) return;
            data[row][col] = value;
        }
    }

    class DeparturesTableModel extends AbstractTableModel {
    	// Three different columns:  User's name, position, and online status
    	String[] columnNames = {"Plane ID", "Destination", "Runway", "Location", "Status"};

    	// Sample data.  Data entered is a two dimensional array of objects.
    	Object[][] data = {
    			{"GFS102","Hobby","KAP303","Runway 3","Landing"},
    			{"SGM304","JFK","SAL200","Runway 4","In Air"},
    	};

    	// Essential functions

        public int getColumnCount() {
        	return columnNames.length;
        }

        public int getRowCount() {
        	if (data == null) return 0;
        	return data.length;
        }

        public String getColumnName(int col) {
        	return columnNames[col];
        }

        // Returns the object at the indicated position
        public Object getValueAt(int row, int col) {
        	if (data == null) return null;
            return data[row][col];
        }

        // Turns booleans into checkboxes.  Don't ask.  I don't know how it works myself.
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        // The table cannot be edited by the user, no matter what.
        public boolean isCellEditable(int row, int col) {
           	return false;
        }

        // Edits the value at the indicated position
        public void setValueAt(Object value, int row, int col) {
        	if (data == null) return;
            data[row][col] = value;
        }
    }
}
